package fr.neyb.bernouilli.problem008;

import java.io.IOException;
import java.io.Reader;

public class ProdReader {

	private final Reader r;
	private final int[] content = new int[] { 0, 0, 0, 0, 0 };
	private int currPos = 0;

	public ProdReader(Reader r) {
		this.r = r;
	}

	public int next() throws NumberFormatException, IOException {
		int c = r.read();
		if (c > 0) {
			return next(Integer.parseInt("" + (char)c));
		} else {
			return -1;
		}
	}

	private int next(int digit) {
		content[currPos] = digit;
		currPos = ++currPos % content.length;
		return getProd();
	}

	private int getProd() {
		int res = 1;
		for (int curr : content) {
			res *= curr;
		}
		return res;
	}

}
